//
//  JLCityViewController.m
//  Liang美团HD
//
//  Created by sun on 16/1/21.
//  Copyright © 2016年 sun. All rights reserved.
//

#import "JLCityViewController.h"
#import "UIBarButtonItem+JLBarButtonItem.h"
#import "JLData.h"
#import "JLCiteGroup.h"
#import <PureLayout.h>
#import "JLCityResultController.h"
#import "JLCities.h"
static NSString * const ID = @"citycell";
@interface JLCityViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong) JLCityResultController * cityResultVc;
/**
 *  蒙版遮罩
 */
@property(nonatomic,strong)UIView * mbCover;
@end

@implementation JLCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"切换城市";
    self.searchBar.delegate = self;
    self.searchBar.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithnorImage:@"btn_navigation_close" highImage:@"btn_navigation_close_hl" target:self action:@selector(close)];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    self.tableView.sectionIndexColor = [UIColor blackColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   
   
}
- (JLCityResultController *)cityResultVc{
    if (_cityResultVc == nil) {
        _cityResultVc = [[JLCityResultController alloc]init];
        [self addChildViewController:_cityResultVc];
        [self.view addSubview:_cityResultVc.view];
        [_cityResultVc.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
        [_cityResultVc.view autoPinEdge:ALEdgeTop toEdge: ALEdgeTop ofView:self.tableView];
    }
    return _cityResultVc;
}
-(UIView *)mbCover{
    if (_mbCover == nil) {
        _mbCover = [[UIView alloc]init];
        _mbCover.backgroundColor = [UIColor blackColor];
        _mbCover.alpha = 0.4;
        [self.view addSubview:_mbCover];
        [_mbCover autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
        [_mbCover autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.tableView];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self.searchBar action:@selector(endEditing:)];
        [_mbCover addGestureRecognizer:tap];
        
    }
    return _mbCover;
}
#pragma mark /****************searchbar代理方法****************/
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [self.searchBar endEditing:YES];
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    self.searchBar.backgroundImage = [UIImage imageNamed:@"bg_login_textfield_hl"];
    [self.searchBar setShowsCancelButton:YES animated:YES];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.mbCover.hidden = NO;
    
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    self.cityResultVc.view.hidden = (self.searchBar.text.length == 0);
    self.cityResultVc.searchText = searchText;
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    self.searchBar.backgroundImage = [UIImage imageNamed:@"bg_login_textfield"];
    [self.searchBar setShowsCancelButton:NO animated:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.mbCover.hidden = YES ;
    self.searchBar.text = nil;
}
-(void)close{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [JLData cityGroupsArray].count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    JLCiteGroup * cityGruop = [JLData cityGroupsArray][section];

    return cityGruop.cities.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    JLCiteGroup * cityGruop = [JLData cityGroupsArray][indexPath.section];
    cell.textLabel.text = cityGruop.cities[indexPath.row];
    return cell;
}
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [[JLData cityGroupsArray] valueForKeyPath:@"title"];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    JLCiteGroup * cityGruop = [JLData cityGroupsArray][section];
    return cityGruop.title;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     [self dismissViewControllerAnimated:YES completion:nil];
     JLCiteGroup * cityGruop = [JLData cityGroupsArray][indexPath.section];
    JLCities * cities = [JLData searchCitiesWithName:cityGruop.cities[indexPath.row]];
     [[NSNotificationCenter defaultCenter] postNotificationName:JLCityDidChangeNotification object:cities];
    
}
@end
